function [] = htmlconv(old_dir, new_dir);

% HTMLCONV - convert directories in links (from '\' to '/')
%
%    HTMLCONV(OLD, NEW) reads HTML files in OLD, converts '\'
%    to '/' in all file links and saves the updated file in NEW.

% parameters

file_spec = '*.htm';
ref_token = 'href=';
src_token = 'src=';

% validate arguments

if (nargin ~= 2)
   msgbox('Invalid number of arguments', 'HTMLCONV Error', 'warn');
   return
elseif (~ischar(old_dir))
   msgbox('OLD must be a string', 'HTMLCONV Error', 'warn');
   return
elseif (~exist(old_dir, 'dir'))
   msgbox('OLD does not exist', 'HTMLCONV Error', 'warn');
   return
elseif (~ischar(new_dir))
   msgbox('NEW must be a string', 'HTMLCONV Error', 'warn');
   return
elseif (~exist(new_dir, 'dir'))
   msgbox('NEW does not exist', 'HTMLCONV Error', 'warn');
   return
end

% loop through files in directory

sdir = dir([old_dir '\' file_spec]);
h_wait = waitbar(0, 'Processing files ...');
for i = 1 : length(sdir)
   
   % open files for reading & writing
   
   read_fid = fopen([old_dir '\' sdir(i).name], 'rt');
   if (read_fid == -1)
      msgbox('HTML file to read does not exist', 'HTMLCONV Error', 'warn');
      break
   end
   write_fid = fopen([new_dir '\' sdir(i).name], 'wt');
   if (write_fid == -1)
      msgbox('HTML file to write already exists', 'HTMLCONV Error', 'warn');
      break
   end
   
   % read contents of file
   % find replace '\' with '/' in links
   % write updated lines
   
   [line, line_cnt] = next_line(0, read_fid);
   while (~isempty(line))
      ref_pos = findstr(line, ref_token);
      if (~isempty(ref_pos))
         line = replace_slash(line, ref_pos(1) + length(ref_token));
      end
      src_pos = findstr(line, src_token);
      if (~isempty(src_pos))
         line = replace_slash(line, src_pos(1) + length(src_token));
      end
      fprintf(write_fid, '%s\n', line);
      [line, line_cnt] = next_line(line_cnt, read_fid);
   end
   
   fclose(read_fid);
   fclose(write_fid);
   waitbar(i / length(sdir));
end
close(h_wait);
return



% NEXT_LINE - get the next line in the file

function [new_line, new_cnt] = next_line(old_cnt, fid);

% initialize outputs

new_line = '';
new_cnt = old_cnt;

% obtain next line if line is empty
% check for EOF when obtaining next line

while (isempty(deblank(new_line)))
   new_line = fgetl(fid);
   if (feof(fid) | (new_line == -1))
      new_line = '';
      return
   else
      new_cnt = new_cnt + 1;
   end
end
return



% REPLACE_SLASH - convert '\' to '/' at specified position

function new_line = replace_slash(old_line, pos);

% initialize output

new_line = old_line;

% find " ... " after 'href=' or 'src='
% replace '\' with '/' within quotes

if (length(new_line) >= pos)
   quote_pos = findstr(new_line, '"');
   if (~isempty(quote_pos))
      quote_pos = quote_pos(find(quote_pos >= pos));
      if (length(quote_pos) >= 2)
         new_line(quote_pos(1) : quote_pos(2)) = strrep(new_line(quote_pos(1) : quote_pos(2)), '\', '/');
      end
   end
end
return
